/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "dma.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
#include "adc.h"
#include "dac.h"
#include "Anx.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */
const static char TAG[] = "RTOS";
RAM_D1 uint16_t temp[100]=
{ 0,      4,     16,     36,     64,    100,    143,    194,    252,    317,
   389,    468,    553,    643,    739,    840,    946,   1055,   1169,   1286,
  1406,   1528,   1653,   1778,   1905,   2032,   2158,   2285,   2410,   2533,
  2654,   2773,   2888,   3000,   3108,   3211,   3310,   3403,   3491,   3572,
  3647,   3716,   3778,   3832,   3879,   3919,   3951,   3975,   3991,   3999,
  3999,   3991,   3975,   3951,   3919,   3879,   3832,   3778,   3716,   3647,
  3572,   3491,   3403,   3310,   3211,   3108,   3000,   2888,   2773,   2654,
  2533,   2410,   2285,   2158,   2032,   1905,   1778,   1653,   1528,   1406,
  1286,   1169,   1055,    946,    840,    739,    643,    553,    468,    389,
   317,    252,    194,    143,    100,     64,     36,     16,      4,      0,
};

RAM_D1 uint16_t adc_buf[100];



/* USER CODE END Variables */
osThreadId defaultTaskHandle;

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */
void test_task(void const* argument);
void pwm_task(void const* argument);
void adc_task(void const* argument);


/* USER CODE END FunctionPrototypes */

void StartDefaultTask(void const * argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/* GetIdleTaskMemory prototype (linked to static allocation support) */
void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );

/* Hook prototypes */
void configureTimerForRunTimeStats(void);
unsigned long getRunTimeCounterValue(void);

/* USER CODE BEGIN 1 */
/* Functions needed when configGENERATE_RUN_TIME_STATS is on */
__weak void configureTimerForRunTimeStats(void)
{

}

__weak unsigned long getRunTimeCounterValue(void)
{
    return 0;
}
/* USER CODE END 1 */

/* USER CODE BEGIN GET_IDLE_TASK_MEMORY */
static StaticTask_t xIdleTaskTCBBuffer;
static StackType_t xIdleStack[configMINIMAL_STACK_SIZE];

void vApplicationGetIdleTaskMemory(StaticTask_t** ppxIdleTaskTCBBuffer, StackType_t** ppxIdleTaskStackBuffer, uint32_t* pulIdleTaskStackSize)
{
    *ppxIdleTaskTCBBuffer = &xIdleTaskTCBBuffer;
    *ppxIdleTaskStackBuffer = &xIdleStack[0];
    *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
    /* place for user code */
}
/* USER CODE END GET_IDLE_TASK_MEMORY */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */
    Debug_log(TAG, "freertos init");

    HAL_ADCEx_Calibration_Start(&hadc1, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED); /* ADC校准 */

    // Anx_Sys_init();
    // HAL_TIM_Base_Start_IT(&htim7);
    HAL_TIM_Base_Start(&htim7);
    HAL_DAC_Start_DMA(&hdac1, DAC_CHANNEL_1, (uint32_t*)temp, 100, DAC_ALIGN_12B_R);
  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
      /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
      /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
      /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
      /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* definition and creation of defaultTask */
  osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 128);
  defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);

  /* USER CODE BEGIN RTOS_THREADS */
            /* add threads, ... */
    osThreadDef(testTask, test_task, osPriorityNormal, 0, 128);
    osThreadCreate(osThread(testTask), NULL);

    osThreadDef(adcTask, adc_task, osPriorityNormal, 0, 128);
    osThreadCreate(osThread(adcTask), NULL);

    // osThreadDef(pwmTask, pwm_task, osPriorityNormal, 0, 128);
    // osThreadCreate(osThread(pwmTask), NULL);
  /* USER CODE END RTOS_THREADS */

}

/* USER CODE BEGIN Header_StartDefaultTask */
/**
  * @brief  Function implementing the defaultTask thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void const * argument)
{
  /* USER CODE BEGIN StartDefaultTask */
            /* Infinite loop */
    for (;;)
    {
        osDelay(500);
        // HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0);
    }
  /* USER CODE END StartDefaultTask */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

void test_task(void const* argument)
{
    HAL_DAC_Start(&hdac1, DAC_CHANNEL_1);
    /* USER CODE BEGIN 5 */
    /* Infinite loop */
    for (;;)
    {
        // osDelay(1000);


        HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_1);

        for (int i = 0; i < 100; i++)
        {
            // HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, (uint32_t)temp[i]); /* 12位右对齐数据格式设置DAC值 */
            // osDelay(10);
        }

    }
    /* USER CODE END 5 */
}

uint16_t adc_value;
void adc_task(void const* argument)
{
    /* USER CODE BEGIN 6 */
    /* Infinite loop */
    for (;;)
    {
        osDelay(1000);


        if(HAL_ADC_Start_DMA(&hadc1, (uint32_t*)&adc_buf, 100) != HAL_OK)
        {
            Debug_log("ADC", "DMA start failed");
        }
        else
        {
            Debug_log("ADC", "adc dma start");
        }

        // HAL_ADC_Start(&hadc1);

        // HAL_ADC_PollForConversion(&hadc1, 10);           /* 轮询转换 */
        // adc_value = HAL_ADC_GetValue(&hadc1);

    }
    /* USER CODE END 6 */
}

void pwm_task(void const* argument)
{
    // __HAL_TIM_ENABLE_IT(&htim8, TIM_IT_UPDATE);
    // HAL_TIM_PWM_Start_IT(&htim8, TIM_CHANNEL_1);
    for (;;)
    {
        Debug_log("PWM", "start");
        // HAL_TIMEx_PWMN_Start_IT(&htim8, TIM_CHANNEL_1);
        spwm_start_default();
        osDelay(1000);
    }
}

/* USER CODE END Application */
